package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.system.domain.bo.SysMaterialStatusAddBo;
import com.xjy.system.domain.bo.SysMaterialStatusEditBo;
import com.xjy.system.domain.bo.SysMaterialStatusQueryBo;
import com.xjy.system.domain.vo.SysMaterialStatusVo;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.SysMaterialStatus;
import com.xjy.system.mapper.SysMaterialStatusMapper;
import com.xjy.system.service.ISysMaterialStatusService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物料状态信息Service业务层处理
 *
 * @author xjy
 * @date 2022-01-30
 */
@Service
public class SysMaterialStatusServiceImpl extends ServicePlusImpl<SysMaterialStatusMapper, SysMaterialStatus> implements ISysMaterialStatusService {

    @Override
    public SysMaterialStatusVo queryById(Long id){
        return getVoById(id, SysMaterialStatusVo.class);
    }

    @Override
    public TableDataInfo<SysMaterialStatusVo> queryPageList(SysMaterialStatusQueryBo bo) {
        PagePlus<SysMaterialStatus, SysMaterialStatusVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysMaterialStatusVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysMaterialStatusVo> queryList(SysMaterialStatusQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysMaterialStatusVo.class);
    }

    private LambdaQueryWrapper<SysMaterialStatus> buildQueryWrapper(SysMaterialStatusQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysMaterialStatus> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStatusNo() != null, SysMaterialStatus::getStatusNo, bo.getStatusNo());
        lqw.like(StrUtil.isNotBlank(bo.getName()), SysMaterialStatus::getName, bo.getName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysMaterialStatusAddBo bo) {
        SysMaterialStatus add = BeanUtil.toBean(bo, SysMaterialStatus.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysMaterialStatusEditBo bo) {
        SysMaterialStatus update = BeanUtil.toBean(bo, SysMaterialStatus.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysMaterialStatus entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
